package test;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import org.apache.log4j.Logger;

/**
 * @author Sinisa
 * 
 */
public class FileContentManipulationUtils {
	
	private static final Logger LOG = Logger.getLogger(FileContentManipulationUtils.class);
	
	/**
	 * Private constructor. Take the instance from the getInstanceMethod.
	 */
	private FileContentManipulationUtils() {
		
	}

	/**
	 * Writes the line to the file. If the file doesn't exist, it gets created.
	 * 
	 * @param file
	 *            the file to write to
	 * @param line
	 *            the line to be written
	 * @param append should the text be appended to the end of the file
	 * @throws IOException
	 */
	public static final void writeLineToFile(File file, String line, boolean append)
			throws IOException {

		if (!file.exists()) {
			LOG.debug("The file " + file.getAbsolutePath()
					+ " doesnt exist, and will be created.");
			file.createNewFile();
			LOG.debug("The file " + file.getAbsolutePath()
					+ " successfully created.");
		}

		PrintWriter writer = new PrintWriter(new FileWriter(file, append));
		writer.println(line);
		writer.flush();
		writer.close();
		
//		LOG.trace("Writen to file: " + file.getName() + " append: "+append+" line: "
//				+ line);
	}
	
}
